﻿

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.BaseProvider;
using VOL.Core.DBManager;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using VOL.Entity.SystemModels;

namespace VOL.Core.Filters
{
    public  class ImportHelper
    {
        
        public string CheckOvertimeImport(string filename ,List<Overtime> OvertimeList)
        {
            string message = "";
            int Row = 1;
            List<int > Rows = new List<int>();
            var dapper = DBServerProvider.GetSqlDapper();
            string departname = OvertimeList[0].departname.ToString();
            string sql = $"select * from overtime_rule where departname = '{departname}'";
            var otr = dapper.QueryFirst<Overtime_rule>(sql, null);
            foreach (var item in OvertimeList)
            {
                DateTime starttime = Convert.ToDateTime(item.starttime);
                DateTime endtime = Convert.ToDateTime(item.endtime);
                //int hours = endtime.Subtract(starttime).Hours;

                TimeSpan timeSpan = endtime - starttime;
                int hours = (int)timeSpan.TotalHours;

                
                
                if (Convert.ToInt32(otr.hours) < hours)
                {
                    Rows.Add(Row);
                    
                }
                Row++;
            }

            if(Rows.Count > 0)
            {
                message += "第";
                foreach(var item in  Rows)
                {
                    message += item;
                    message += "，";
                }
                message += " 行时间填写不符合规范";
            }
            return message;
        }

        public string CheckOvertimeLineImport(string filename, List<new_overtimeline> OvertimeLineList)
        {
            string message = "";
            int Row = 1;
            List<int> Rows = new List<int>();
            var dapper = DBServerProvider.GetSqlDapper();
            
            foreach (var item in OvertimeLineList)
            {
                var new_overtime_id = item.new_overtime_id;
                string sql = @$"select * from overtime_rule where departname = 
                    (select new_depart from new_overtime where new_overtime_id = '{new_overtime_id}')";
                var otr = dapper.QueryFirst<Overtime_rule>(sql, null);
                DateTime starttime = Convert.ToDateTime(item.new_starttime);
                DateTime endtime = Convert.ToDateTime(item.new_endtime);
                //int hours = endtime.Subtract(starttime).Hours;

                TimeSpan timeSpan = endtime - starttime;
                int hours = (int)timeSpan.TotalHours;



                if (Convert.ToInt32(otr.hours) < hours)
                {
                    Rows.Add(Row);

                }
                Row++;
            }

            if (Rows.Count > 0)
            {
                message += "第";
                foreach (var item in Rows)
                {
                    message += item;
                    message += "，";
                }
                message += " 行时间填写不符合规范";
            }
            return message;
        }
    }
}
